package handlers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"shopback/models"
	"strconv"
	"time"

	"github.com/gorilla/mux"
)

// AddToFavorite 向收藏夹中添加商品
	// @Summary 向收藏夹中添加商品
	// @Description 根据用户ID和商品ID将商品加入收藏夹
	// @Tags 收藏夹
	// @Accept json
	// @Produce json
	// @Param user_id path int true "用户ID"
	// @Param product_id path int true "商品ID"
	// @Success 201 {object} map[string]string "商品收藏成功"
	// @Failure 400 {string} string "无效的请求参数"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /favorite/{user_id}/products/{product_id} [post]
func AddToFavorite(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	userID, err := strconv.Atoi(vars["user_id"])
	if err!= nil {
		w.Header().Set("Content-Type", "application/json")
		http.Error(w, `{"message": "无效的用户ID"}`, http.StatusBadRequest)
		return
	}

	// 获取商品ID
	productID, err := strconv.Atoi(vars["product_id"])
	if err!= nil || productID <= 0 {
		w.Header().Set("Content-Type", "application/json")
		http.Error(w, `{"message": "无效的商品ID"}`, http.StatusBadRequest)
		return
	}

	// 调用模型层的 AddToFavorite 函数来添加商品到收藏夹
	favoriteID, err := models.AddToFavorite(userID, productID)
	if err!= nil {
		w.Header().Set("Content-Type", "application/json")
		http.Error(w, fmt.Sprintf(`{"message": "%s"}`, err.Error()), http.StatusInternalServerError)
		return
	}

	// 返回成功消息
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(map[string]interface{}{
		"message": "商品收藏成功",
		"favorite_id": favoriteID,
		"product_id": productID,
		"更新时间": time.Now(),
	})
}

// GetFavorite 获取用户的收藏夹
	// @Summary 获取用户的收藏夹
	// @Description 根据用户ID获取收藏夹及收藏的商品信息
	// @Tags 收藏夹
	// @Accept json
	// @Produce json
	// @Param user_id path int true "用户ID"
	// @Success 200 {object} models.Favorite "收藏夹信息"
	// @Failure 400 {string} string "无效的用户ID"
	// @Failure 404 {string} string "收藏夹未找到"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /favorite/{user_id} [get]
func GetFavorite(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	// 获取用户ID
	userID, err := strconv.Atoi(vars["user_id"])
	if err != nil {
		http.Error(w, `{"message": "无效的用户ID"}`, http.StatusBadRequest)
		return
	}

	// 获取收藏夹信息
	favorite, favoriteItems, err := models.GetFavoriteByUserID(userID)
	if err != nil {
		http.Error(w, `{"message": "获取收藏夹失败"}`, http.StatusInternalServerError)
		return
	}

	// 如果收藏夹为空，返回空收藏夹
	if favorite.UserID == 0 {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]interface{}{
			"favorite":       models.Favorite{UserID: userID},
			"favorite_items": []models.FavoriteItem{},
		})
		return
	}

	// 返回收藏夹信息
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]interface{}{
		"favorite":       favorite,
		"favorite_items": favoriteItems,
	})
}

// DeleteFromFavorite 从收藏夹中删除商品
	// @Summary 从收藏夹中删除商品
	// @Description 根据用户ID和商品ID将商品从收藏夹中移除
	// @Tags 收藏夹
	// @Accept json
	// @Produce json
	// @Param user_id path int true "用户ID"
	// @Param product_id path int true "商品ID"
	// @Success 200 {object} map[string]string "商品移除成功"
	// @Failure 400 {string} string "无效的请求参数"
	// @Failure 404 {string} string "商品不存在于收藏夹中"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /favorite/{user_id}/products/{product_id} [delete]
func DeleteFromFavorite(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	// 获取用户ID
	userID, err := strconv.Atoi(vars["user_id"])
	if err != nil {
		w.Header().Set("Content-Type", "application/json")
		http.Error(w, `{"message": "无效的用户ID"}`, http.StatusBadRequest)
		return
	}

	// 获取商品ID
	productID, err := strconv.Atoi(vars["product_id"])
	if err!= nil {
		w.Header().Set("Content-Type", "application/json")
		http.Error(w, `{"message": "无效的商品ID"}`, http.StatusBadRequest)
		return
	}

	// 调用 models 层的 DeleteFromFavorite 函数来删除商品
	rowsAffected, err := models.DeleteFromFavorite(userID, productID)
	if err != nil {
		w.Header().Set("Content-Type", "application/json")
		http.Error(w, fmt.Sprintf(`{"message": "%s"}`, err.Error()), http.StatusInternalServerError)
		return
	}

	if rowsAffected == 0 {
		w.Header().Set("Content-Type", "application/json")
		http.Error(w, `{"message": "商品不存在于收藏夹中"}`, http.StatusNotFound)
		return
	}

	// 返回成功消息
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]interface{}{
		"message": "商品移除成功",
	})
}